package com.front.web.mall;

import com.bootdo.common.utils.*;
import com.bootdo.common.wxpay.CommonUtil;
import com.bootdo.common.wxpay.WxPayHelper;
import com.bootdo.common.wxpay.XMLUtils;
import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.haojiangboutils.ResultReBuild;
import com.bootdo.lvyou.domain.LvyouSpecialtyEnclosure;
import com.bootdo.mall.domain.*;
import com.bootdo.member.domain.ShequMember;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

@RequestMapping("/f/mall/index/v1")
@RestController
public class MallIndexControllerV1 extends MallSupperController {

    /**
     * http://127.0.0.1:8090/f/mall/index/v1/getIndexCategory
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("getIndexCategory")
    public R getCategory(
            HttpServletRequest request,
                         HttpServletResponse response){
        //导航图标
        List<MallIndexCategory> daohang =  mallIndexCategoryService
                .list(MapBuild.getInstand().setKV("area",1));
        //推荐
        List<MallIndexCategory> tuijian =  mallIndexCategoryService
                .list(MapBuild.getInstand().setKV("area",2));
        //底部分类
        List<MallIndexCategory> fenlei =  mallIndexCategoryService
                .list(MapBuild.getInstand().setKV("area",3));

        return R.ok().put("daohang",daohang).put("tuijian", tuijian).put("fenlei", fenlei);
    }


    /**
     * http://127.0.0.1:8090/f/mall/index/v1/getGoodList?pageNo=0&pageSize=10&id=13&type=2
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("getGoodList")
    public R getGoodList(
            @RequestParam("id") Long id,
            @RequestParam("type") Integer type,
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam("pageSize") Integer pageSize,
            HttpServletRequest request,
            HttpServletResponse response){

        Map query =  MapBuild.getInstand();
        //.setKV("categoryId", categoryId)
        //说明该商品关联的是分类
        List<Long> goodIdLis = new ArrayList<>();
        if(type.intValue() == 1){
          List<MallIndexCategoryJoin> mallIndexCategoryJoins = mallIndexCategoryJoinService.list(MapBuild.getInstand()
                  .setKV("indexCategoryId", id).setKV("type", 1));
          query.put("categoryId",mallIndexCategoryJoins.get(0).getRefId());
        }else if(type.intValue() == 2){
            List<MallIndexCategoryJoin> mallIndexCategoryJoins = mallIndexCategoryJoinService.list(MapBuild.getInstand()
                    .setKV("indexCategoryId", id).setKV("type", 2));
            /*query.put("categoryId",mallIndexCategoryJoins.get(0).getRefId());*/
            mallIndexCategoryJoins.stream().forEach(item -> {
                goodIdLis.add(item.getRefId());
            });
            query.put("goodIdList",goodIdLis);
            query.put("indexCategoryId", id);
        }
        String field = request.getParameter("field");
        String sort = request.getParameter("sort");
        if(StringUtils.isEmpty(field)){
            field = "salesNum";
        }
        if(StringUtils.isEmpty(sort)){
            sort = "desc";
        }
        query.put("field",field);
        query.put("sort",sort);
        query.put("pageNo",pageNo * pageSize);
        query.put("pageSize",pageSize);

        int count =  mallGoodService.getGoodCountByIndex(query);
        int pageSum=(count-1)/pageSize+1;
        if(pageNo > pageSum-1){
            return R.error("没有更多了");
        }
        List<MallGood>  mallGoods  =  mallGoodService.getGoodListByIndex(query);
        if(mallGoods.size() <= 0){
            return R.error("没有更多了");
        }
        return R.ok().put("l", mallGoods);
    }


}
